package com.sinodata.bsm.cicp.collector.tomcat;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.collector.Collector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.cicp.util.JmxUtil;
import com.sinodata.bsm.common.vo.ResPropertyValue;

public class CacheCollector extends Collector {
    private static Logger logger = Logger.getLogger(CacheCollector.class);

    @Override
    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        String port = this.getParamMap().get("Port").trim();

        String[] propertyNames = new String[] { "accessCount", "hitsCount", "cacheSize", "cacheMaxSize" };
        Object propertyValues[] = null;
        String cacheName = this.getParamMap().get("Name").trim();
        String[] result = new String[6];
        String name = null;

        List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();
        String[] propMetas = getPropMeta();
        ResPropertyValue propertyValueX = new ResPropertyValue();
        ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[propMetas.length];
        JMXConnector connector = null;
        MBeanServerConnection connection = null;
        try {
            //connector = JmxUtil.getConnection(JmxUtil.TOMCAT, ip + ":" + port);
            connector = JmxUtil.getConnection(ip, Integer.parseInt(port));
            connection = connector.getMBeanServerConnection();
            ObjectName tomcatThreadPool = new ObjectName("Catalina:type=Cache,*");
            Set<ObjectName> set = connection.queryNames(tomcatThreadPool, null);
            for (ObjectName objectName : set) {
                name = objectName.getKeyProperty("path");
                if (name.equals(cacheName)) {
                    propertyValues = JmxUtil.getAttribute(connection, objectName, propertyNames);
                    String[] temp = new String[propertyValues.length];
                    for (int i = 0; i < propertyValues.length; i++) {
                        temp[i] = propertyValues[i].toString();
                        propValues[i] = ResPropertyValue.createPropValue(temp[i], propMetas[i]);

                    }
                    System.arraycopy(temp, 0, result, 0, 4);
                }
            }
        } catch (Exception e) {
            logger.error("when ThreadPoolFinder " + e.getMessage());
            throw new CollectorException(e);
        } finally {
            JmxUtil.closeConnector(connector);
        }
        if ((result[0] != null) && (result[1] != null) && (result[2] != null) && (result[3] != null)) {
            if (Long.parseLong(result[0]) > 0) {
                result[4] = String.valueOf(Long.parseLong(result[1]) * 100 / Double.parseDouble(result[0]));
            } else {
                result[4] = "0";
            }
            if (Long.parseLong(result[3]) > 0) {
                result[5] = String.valueOf(Long.parseLong(result[2]) * 100 / Double.parseDouble(result[3]));
            } else {
                result[5] = "0";
            }
            propValues[4] = ResPropertyValue.createPropValue((result[4]), propMetas[4]);
            propValues[5] = ResPropertyValue.createPropValue((result[5]), propMetas[5]);
        }
        propertyValueX.setValues(propValues);
        values.add(propertyValueX);
        return values;
    }

    @Override
    public String[] getParamMeta() {
        return new String[] { "Name", "Port" };
    }

    @Override
    public String[] getPropMeta() {
        //return new String[] { "AccessTimes", "HitTimes", "CurrentCacheSize", "MaxCacheSize", "HitRatio", "Usage" };
        return new String[] { "$1", "$2", "$3", "$4", "$5", "$6" };
    }

}
